<?php
namespace Index\Controller;
use Think\Controller;
/**
 * 搜索类
 */
class SoController extends CommonController {

    public function index()
    {
        $q=I('get.q');

        $words=getwords($q);

         $sql="select * from question where ";
        if ($words) {
        foreach ($words as $row) {
            //组以个sql语句
            $sql .= ' title like "%'.$row['word'].'%"or';
        } 
            $sql=rtrim($sql,'or');

        }else{
           $sql .= ' title like "%'.$q.'%"';
           // var_dump($sql);
           // return $data=array('tilte'=>"搜索不到你要找的");        
        }
        $data=M()->query($sql);
        $a=$data['title'];
        
        // var_dump($data);
        // 关键字变红
        foreach ($data as &$v) {
            foreach ($words as $k) {
                $v['title']=str_replace($k[word],"<font color='red'>".$k[word]."</font>",$v['title']); 
                $v['content']=str_replace($k[word],"<font color='red'>".$k[word]."</font>",$v['content']); 
            }
        }

        // var_dump($data);
        $this->assign('data',$data);
        $this->assign('q',$q);
        $this->display();

 }





}



?>